package org.web2024.cnofig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.web2024.utils.FastJsonRedisSerializer;

@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        // 创建一个RedisTemplate实例，用于操作Redis数据库
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        // 设置Redis连接工厂，用于管理与Redis服务器的连接
        template.setConnectionFactory(redisConnectionFactory);
        // 创建一个Jackson2JsonRedisSerializer实例，用于序列化和反序列化Redis中的JSON数据
        //Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
        //GenericJackson2JsonRedisSerializer Serializer=new GenericJackson2JsonRedisSerializer();
        FastJsonRedisSerializer<Object> Serializer=new FastJsonRedisSerializer<>(Object.class);

        template.setKeySerializer(new StringRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(Serializer);
        // 设置值的序列化器为Jackson2JsonRedisSerializer，以便将对象序列化为JSON格式存储
        template.setValueSerializer(Serializer);

        template.afterPropertiesSet();
        // 返回配置好的RedisTemplate实例
        return template;
    }
}
